<?php

namespace App\Http\Controllers\Api;

use App\Models\Activity;
use App\Models\ActivityUsers;
use App\Models\ChapterQuestion;
use App\Models\Collection;
use App\Models\Dict;
use App\Models\Goods;
use App\Models\Integral;
use App\Models\IntegralUsers;
use App\Models\LmChapter;
use App\Models\LmColumn;
use App\Models\LmSubject;
use App\Models\PapersQuestion;
use App\Models\Smsrecord;
use App\Models\Users;
use App\Models\YtkCourse;
use App\Models\YtkCourseVideo;
use App\Models\YtkDouyin;
use App\Models\YtkDownload;
use App\Models\YtkIntegral;
use App\Models\YtkIntegralTask;
use App\Models\YtkKnowledgeCate;
use App\Models\YtkMindmap;
use App\Models\YtkOrders;
use App\Models\YtkSquare;
use App\Models\YtkSquareComment;
use App\Models\YtkUsers;
use App\Models\YtkUsersCollect;
use App\Models\YtkUsersCourse;
use App\Models\YtkUsersErrors;
use App\Models\YtkUsersListen;
use App\Models\YtkUsersListenDetails;
use App\Models\YtkUsersPapers;
use App\Models\YtkUsersQuestion;
use App\Models\YtkUsersTickets;
use App\Models\YtkVip;
use App\Models\YtkVipLogs;
use App\Models\YtkVipUsers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class UsersController extends Controller
{
    //用户信息
    public function index()
    {
        $user = Users::where('id', $this->uId)->first();
        if (empty($user['headImgUrl'])) {
            $user['headImgUrl'] = "https://cloud.wxstores.com/miapp/defaultHead.jpg";
        }
        if ($user['polity_id']) {
            $user['polityName'] = Dict::where(['dictname' => 'polity', 'dvalue' => $user['polity_id']])->value('dkey');
        }
        if ($user['jobs_id']) {
            $user['jobsName'] = Dict::where(['dictname' => 'jobs', 'dvalue' => $user['jobs_id']])->value('dkey');
        }
        if ($user['industry_id']) {
            $user['industryName'] = Dict::where(['dictname' => 'industry', 'dvalue' => $user['industry_id']])->value('dkey');
        }
        if ($user['service_id']) {
            $user['serviceName'] = Dict::where(['dictname' => 'service', 'dvalue' => $user['service_id']])->value('dkey');
        }
        if ($user['sex'] == 1) {
            $user['sexName'] = "男";
        } else {
            $user['sexName'] = "女";
        }

        //活动
        $activity = ActivityUsers::where('uid', $this->uId)->count();
        //手册
        $collect = Collection::where('uid', $this->uId)->where('type', 1)->count();
        $user['activity'] = $activity;
        $user['collect'] = $collect;

        return showMsg('获取成功', 200, $user);
    }

    //更新名片
    public function setUserInfo(Request $request)
    {
        $birthday = $request->get('birthday');
        $data = $request->only('address', 'industry_id', 'jobs_title', 'jobs_id', 'mobile', 'nickname', 'polity_id', 'service_desc', 'service_id', 'sex', 'company');
        if ($birthday) {
            if(is_int($birthday)) {
                $data['birthday'] = date('Y-m-d', $birthday / 1000);
            }
        }
        Users::where('id', $this->uId)->update($data);
        return showMsg('更新成功', 200, $data);
    }

    //我的收藏
    public function myCollect(Request $request)
    {
        $model = Collection::query();
        $model = $model->with('article');
        $model = $model->where('uid', $this->uId);
        $model = $model->where('type', 1);
        $model = $model->orderBy('created_at', 'desc');
        $course = $model->paginate(10);
        return showMsg('获取成功！', 200, $course);
    }

    //我的积分
    public function myIntegral()
    {
        $total = Users::where('id', $this->uId)->value('integral');
        $array['total'] = $total;
        $today = IntegralUsers::where(['user_id' => $this->uId, 'status' => 1])->sum('integral');
        $array['today'] = $today;
        $integral = Integral::get();
        $array['integral'] = $integral;
        return showMsg('获取成功！', 200, $array);
    }

    //我的名片
    public function myBusiness(Request $request)
    {
        $uid = $request->get('id', 0);
        if (empty($uid)) {
            $uid = $this->uId;
        }
        $bus = Users::select('id', 'company', 'jobs_title', 'address', 'mobile', 'headImgUrl', 'nickname')->where('id', $uid)->first();
        if (empty($bus['headImgUrl'])) {
            $bus['headImgUrl'] = "https://cloud.wxstores.com/miapp/defaultHead.jpg";
        }
        return showMsg('获取成功！', 200, $bus);
    }

    //会员风采
    public function elegance()
    {
        $result = Dict::where('dictname', 'company_status')->get();
        return showMsg('获取成功！', 200, $result);
    }

    //会员详情
    public function getUserDetails(Request $request)
    {
        $id = $request->get('id');
        $user = Users::where('id', $id)->first();
        if (empty($user['headImgUrl'])) {
            $user['headImgUrl'] = "https://tkxcx.sddsghedu.com/images/logo.png?v=1.0";
        }
        if ($user['polity_id']) {
            $user['polityName'] = Dict::where(['dictname' => 'polity', 'dvalue' => $user['polity_id']])->value('dkey');
        }
        if ($user['jobs_id']) {
            $user['jobsName'] = Dict::where(['dictname' => 'jobs', 'dvalue' => $user['jobs_id']])->value('dkey');
        }
        if ($user['industry_id']) {
            $user['industryName'] = Dict::where(['dictname' => 'industry', 'dvalue' => $user['industry_id']])->value('dkey');
        }
        if ($user['service_id']) {
            $user['serviceName'] = Dict::where(['dictname' => 'service', 'dvalue' => $user['service_id']])->value('dkey');
        }
        if ($user['sex'] == 1) {
            $user['sexName'] = "男";
        } else {
            $user['sexName'] = "女";
        }
        return showMsg('获取成功！', 200, $user);
    }

    public function signQrcode(Request $request)
    {
        $id = $request->get('id');
        $activity = Activity::where('id', $id)->first();
        if ($activity) {
            $activity['time'] = date('Y.m.d', strtotime($activity['stime'])) . '-' . date('m.d', strtotime($activity['etime']));
            return showMsg('获取成功！', 200, $activity);
        } else {
            return showMsg('活动已结束！', 201, $activity);
        }
    }

    //确认计划
    public function confirmSign(Request $request)
    {
        $id = $request->get('id');
        $re = ActivityUsers::where(['activity_id' => $id, 'uid' => $this->uId])->first();
        if ($re) {
            $data['sign_time'] = time();
            $data['sign_status'] = 1;
            ActivityUsers::where(['activity_id' => $id, 'uid' => $this->uId])->update($data);
            return showMsg('获取成功！', 200);
        } else {
            return showMsg('您未报名此活动，无法签到！', 201);
        }
    }


}
